﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SystemHelperLibrary.KeyboardExt
{
	public static class KeybordUtils
	{
		static readonly Dictionary<char, char> Eng2Rus = new Dictionary<char, char>();
		static readonly Dictionary<char, char> Rus2Eng = new Dictionary<char, char>();

		private static readonly char[] EnKeyboard = {
		 'q','w','e','r','t','y','u','i','o','p','[',']',
		 'a','s','d','f','g','h','j','k','l',';','\'',
		 'z','x','c','v','b','n','m',',','.','`'};

		private static readonly char[] RusKeyboard = {
		  'й','ц','у','к','е','н','г','ш','щ','з','х','ъ',
		  'ф','ы','в','а','п','р','о','л','д','ж','э','я',
		  'ч','с','м','и','т','ь','б','ю','ё'};

		static KeybordUtils()
		{
			for (int i = 0; i < 33; i++)
			{
				Rus2Eng.Add(RusKeyboard[i], EnKeyboard[i]);
				Eng2Rus.Add(EnKeyboard[i], RusKeyboard[i]);
			}
		}

		public static string KeyboardSwitch(string text)
		{
			StringBuilder builder = new StringBuilder(text);

			bool isRussian = text.IndexOfAny(RusKeyboard) != -1;

			for (int i = 0; i < text.Length; i++)
			{
				if (isRussian ? Rus2Eng.ContainsKey(text[i]) : Eng2Rus.ContainsKey(text[i]))
				{
					builder[i] = isRussian ? Rus2Eng[text[i]] : Eng2Rus[text[i]];
				}
			}
			return builder.ToString();
		}
	}
}
